package com.datam.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.datam.dao.CompanyInfoDAO;
import com.datam.dao.CompanyPkInfoDAO;
import com.datam.dao.ConpanyTxInfoDAO;
import com.datam.domain.CompanyPkInfoDO;
import com.datam.domain.ConpanyTxInfoDO;
import com.datam.query.QueryCompanyPkInfo;
import com.datam.query.QueryConpanyTxInfo;
import com.datam.utils.DMResult;

@Controller
public class PFController {
	
	@Autowired
	CompanyInfoDAO companyInfoDAO;
	
	@Autowired
	CompanyPkInfoDAO companyPkInfoDAO ; 
	
	@Autowired
	ConpanyTxInfoDAO conpanyTxInfoDAO ;
	
	
	//@RequestMapping("/StainMonitor/GetDataList")
	String nds = "a25002,a25003,a25005,a24088";
	String ndsdw = "毫克/立方米";
	Map<String,String> dwMap = new HashMap<String,String>(){{  
	      put("PFL","毫克/立方米"); 
	      put("ND","毫克/千克"); 
	      put("a25002","苯"); 
	      put("a25003","甲苯");
	      put("a25005","二甲苯"); 
	      put("a24088","非甲烷总烃");
	      put("flowvocs","流量,立方米");
	      put("a01013","烟气压力,千帕"); 
	      put("a01012","烟气温度,摄氏度"); 
	      put("a01011","烟气流速,烟气流速");
	      put("a01014","烟气湿度,%"); 
	      put("a01016","烟道截面积,平方米");
	      put("a19001","含氧量,%");
	}};
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("/StainMonitor/pf")
	public @ResponseBody Object GetDataList(HttpServletRequest request, HttpServletResponse response,
			Long ID, Long TYPE,String BeginTime,String EndTime,String DataType){
		try {
			if("M".equals(DataType))DataType="2051";
			else if ("H".equals(DataType))DataType="2061";
			else if("D".equals(DataType))DataType="2031";
			else DataType="2031";
			if(ID==null)	return new DMResult(null,"ID不能为空",false);
			QueryConpanyTxInfo queryConpanyTxInfo = new QueryConpanyTxInfo();
			queryConpanyTxInfo.setPKID(ID);
			List< ConpanyTxInfoDO > conpanyTxInfoDO = conpanyTxInfoDAO.selectConpanyTxInfoList(queryConpanyTxInfo);
			if(conpanyTxInfoDO==null || conpanyTxInfoDO.size()==0) return new DMResult(null,"无通信设备",false);
			ConpanyTxInfoDO txData = conpanyTxInfoDO.get(0);
			List<Map> pdData = companyInfoDAO.selectPF(DataType,txData.getMN(),BeginTime,EndTime);
			if(pdData==null||pdData.size()==0) return new DMResult(pdData,"操作成功",true);
			List<Map> data = new ArrayList<Map>();
			
			Map rMap = new HashMap();
			Map titleMap = new HashMap();
			rMap.put("TITLE", titleMap);
			
			List<Map> maintitleMaps = new ArrayList();
			titleMap.put("MAINTITLE", maintitleMaps);
			
			List<Map> subtitleMaps = new ArrayList();
			titleMap.put("SUBTITLE", subtitleMaps);
			
			String clsc = txData.getCLCS();
			
			Map<String,Map> qtMaps = new LinkedHashMap<>();
			List<String> times = new ArrayList<String>();
			for(Map map : pdData){
				if(!clsc.contains(map.get("code")+"")){continue;}
				String code = map.get("code")+"";
				String key = map.get("upload_time")+"";
				Map qtMap = qtMaps.get(key);
				if(qtMap==null){
					qtMap = new HashMap();
					qtMaps.put(key, qtMap);
					qtMap.put("RECTIME", map.get("upload_time")+"");
					
					qtMap.put("flowvocs",map.get("avgval"));
					qtMap.put("flowvocs_STATUS","0");
					qtMap.put("flowvocs_UNIT","立方米");
					
					qtMap.put("a01013",map.get("pressure_pol"));
					qtMap.put("a01013_STATUS","0");
					qtMap.put("a01013_UNIT","千帕");
					
					qtMap.put("a01012",map.get("temp_pol"));
					qtMap.put("a01012_STATUS","0");
					qtMap.put("a01012_UNIT","摄氏度");
					
					qtMap.put("a01011",map.get("speed_pol"));
					qtMap.put("a01011_STATUS","0");
					qtMap.put("a01011_UNIT","米/秒");
					
					qtMap.put("a01014",map.get("wet_pol"));
					qtMap.put("a01014_STATUS","0");
					qtMap.put("a01014_UNIT","%");
					
					qtMap.put("a01016","");
					qtMap.put("a01016_STATUS","0");
					qtMap.put("a01016_UNIT","平方米");
					
					qtMap.put("a19001","");
					qtMap.put("a19001_STATUS","0");
					qtMap.put("a19001_UNIT","平方米");
				
					times.add( map.get("upload_time")+"");
				}
				//判断是不是有浓度的气体
				if(nds.contains(code)){
					qtMap.put(code+"_ND", map.get("nd"));
					qtMap.put(code+"_ND_STATUS", "0");
					qtMap.put(code+"_ND_UNIT", "毫克/立方米");
					
					qtMap.put(code+"_PFL", map.get("pfl"));
					qtMap.put(code+"_PFL_STATUS", "0");
					qtMap.put(code+"_PFL_UNIT", "千克");
				}
			}
			 
			Collection srt = qtMaps.values();
			Map dataMap = new HashMap();
			titleMap.put("data", dataMap);
			dataMap.put("total", srt.size());
			dataMap.put("rows", srt);
			//开始评价头部
			String[] clscs= clsc.split(",");
			for(String s : clscs){
				Map maintitleMap = new HashMap();
				if(dwMap.get(s)==null) continue ;
				String[] dw = dwMap.get(s).split(",");
				if(nds.contains(s)){
					maintitleMap.put("ITEMCODE", s);
					maintitleMap.put("ITEMTEXT", dw[0]);
					maintitleMap.put("UNIT","");
					maintitleMap.put("COLSPAN", 2);
					
					Map subtitleMap = new HashMap();
					subtitleMap.put("ITEMCODE", s);
					subtitleMap.put("SUBITEMCODE", "ND");
					subtitleMap.put("SUBITEMTEXT","浓度");
					subtitleMap.put("UNIT", "毫克/立方米");
					Map subtitleMap2 = new HashMap();
					subtitleMap2.put("ITEMCODE", s);
					subtitleMap2.put("SUBITEMCODE", "PFL");
					subtitleMap2.put("SUBITEMTEXT","排放量");
					subtitleMap2.put("UNIT", "千克");
					subtitleMaps.add(subtitleMap2);
					subtitleMaps.add(subtitleMap);
				}else{
					maintitleMap.put("ITEMCODE", s);
					maintitleMap.put("ITEMTEXT", dw[0]);
					maintitleMap.put("UNIT", dw[1]);
					maintitleMap.put("COLSPAN", 1);
				}
				maintitleMaps.add(maintitleMap);
			}
			
			return rMap;
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}

}
